using System.Diagnostics.CodeAnalysis;
using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;

namespace Identity.Account.Compoents;
public abstract class AssignmentBase<TItem> : ComponentBase
{
    [Parameter]
    [EditorRequired]
    [NotNull]
    public List<TItem>? Items { get; set; }

    [Parameter]
    [EditorRequired]
    [NotNull]
    public List<string>? Value { get; set; }

    [Parameter]
    [EditorRequired]
    [NotNull]
    public Action<List<string>>? OnValueChanged { get; set; }

    // 是否启用搜索
    [Parameter]
    public bool EnableSearch { get; set; } = true;

    // 是否启用分组
    [Parameter]
    public Func<TItem, string>? GroupBy { get; set; }

    // 是否显示全选按钮
    [Parameter]
    public bool ShowSelectAll { get; set; } = true;

    // 搜索关键字
    protected string SearchKeyword { get; set; } = string.Empty;

    // 过滤后的数据
    protected IEnumerable<TItem> FilteredItems =>
        string.IsNullOrWhiteSpace(SearchKeyword)
            ? Items ?? Enumerable.Empty<TItem>()
            : Items?.Where(item =>
                (item as SelectedItem)?.Text?.Contains(SearchKeyword, StringComparison.OrdinalIgnoreCase) ?? false
            ).Union(Items?.Where(item =>
                Value.Contains((item as SelectedItem)?.Value)
            )) ?? Enumerable.Empty<TItem>();
}